﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace DataAccess
{
   public  class CustomerDA
    {
        ConnectDB cn = new ConnectDB();
        AccountEntites ace = new AccountEntites();
       // NHẤT

        public static DataTable GetName(CardEntites card)
        {           
            DataTable dt = Data.ExeStoredWithParamToDataTable("ProAccountSelectByCardNo_Nhat", "@CardNo", card.CardNo);
            return dt;
        }
        // NHẤT

        public static DataTable GetNameReceive(AccountEntites a)
        {            
            DataTable dt = Data.ExeStoredWithParamToDataTable("ProAccountSelectByAccountID", "@AccountID", a.AccountID);
            return dt;
        }
        // NHẤT

        public bool Transfer(AccountEntites a)
        {
            bool kt = false;
            try{
                SqlConnection con = new SqlConnection(cn.ConnectionString());
            con.Open();
            SqlCommand cmd = new SqlCommand("ProcTransferAccount", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@AccountTransferID", a.AccountID));
            cmd.Parameters.Add(new SqlParameter("@Amount",a.Balance));
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            kt = true;
            }catch(Exception ex)
            {
                throw ex;
            }            
            return kt;
        }
        // NHẤT

        public bool TransferAccountReceive(AccountEntites a)
        {
            bool kt = false;
            try { 
                 SqlConnection con = new SqlConnection(cn.ConnectionString());
            con.Open();
            SqlCommand cmd = new SqlCommand("ProcTransferAccountReceiveID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@AccountReceiveID", a.AccountID));
            cmd.Parameters.Add(new SqlParameter("@Amount", a.Balance));
            cmd.ExecuteNonQuery();
            cmd.Dispose();
                kt=true;
            }catch(Exception ex)
            {
                throw ex;
            }
           
            return kt;
        }
        // NHẤT

        public decimal checkBalance(CardEntites c)
        {
            try{
                SqlConnection con = new SqlConnection(cn.ConnectionString());
                con.Open();
                SqlCommand cmd = new SqlCommand("SP_CheckBalanceByCarNo", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CardNo", c.CardNo));
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                decimal x = decimal.Parse(dt.Rows[0][0].ToString());
                cmd.Dispose();
                return x;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
            
        }

       // Dương

        public static DataTable ViewHistory_OneWeek(string cardNo)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 7);
            return dt;
        }

        public static DataTable ViewHistory_OneMonth(string cardNo/*CardEntites card*/)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 30);
            return dt;
        }
        public static DataTable ViewHistory_FourMonth(string cardNo)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 120);
            return dt;
        }

        public static DataTable ViewHistory_SixMonth(string cardNo)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 180);
            return dt;
        }

        public static DataTable ViewHistory_OneYear(string cardNo)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 365);
            return dt;
        }

        public static DataTable ViewHistory_TwoYear(string cardNo)
        {

            //card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
            DataTable dt = Data.ExeStoredWithParamToDataTabe_Duong("ViewHistoryByTime", cardNo, 700);
            return dt;
        }

    }
}
